using System;
using System.Collections.Generic;
using FwkLight.CodeExtensions;

namespace FwkLight.Domain.Fetching
{
  public interface IAliasListForHierarchy : ICalculator<IList<string>, IFetchingPart>
  {
  }

  public class AliasListForHierarchy: IAliasListForHierarchy
  {
    private readonly IRootFetchingPartInHierarchy _rootFetchingPartInHierarchy;

    public AliasListForHierarchy(IRootFetchingPartInHierarchy rootFetchingPartCalculator)
    {
      _rootFetchingPartInHierarchy = rootFetchingPartCalculator;
    }

    public IList<string> Calculate(IFetchingPart fetchingPart)
    {
      IList<string> aliasList = new List<string>();
      IFetchingPart root = fetchingPart.CalculateThe(_rootFetchingPartInHierarchy);
      AddAliasesInto(aliasList, root);
      return aliasList;
    }

    private void AddAliasesInto(ICollection<string> aliasList, IFetchingPart fetchingPart)
    {
      aliasList.Add(fetchingPart.Alias);
      fetchingPart.EntityChildren.ForEach(p => AddAliasesInto(aliasList, p));
      fetchingPart.ListChildren.ForEach(p => AddAliasesInto(aliasList, p));
    }
  }
}